package com.ruoyi.dataSet.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.DsZsk;
import com.ruoyi.system.service.IDsZskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Anonymous
@RestController
@RequestMapping("/folder")
public class FolderController {

    @Autowired
    private IDsZskService zskService;

    // 创建文件夹
    @PostMapping("/create")
    public String createFolder(@RequestBody DsZsk folder) {
        try {
            // 设置文件夹标识
            folder.setFileFlag("1");
            // 设置父id为0
            folder.setPId(0L);
            zskService.insertDsZsk(folder);
            return "Folder created successfully";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 获取课程下的所有文件夹
    @GetMapping("/list/{classId}")
    public List<DsZsk> getFoldersByClassId(@PathVariable Long classId) {
        DsZsk query = new DsZsk();
        query.setClassId(classId);
        query.setPId(0L); // 只查询根目录下的文件夹
        query.setFileFlag("1"); // 只查询文件夹
        return zskService.selectDsZskList(query);
    }

    // 获取文件夹下的所有文件
    @GetMapping("/files/{folderId}")
    public List<DsZsk> getFilesByFolderId(@PathVariable Long folderId) {
        DsZsk query = new DsZsk();
        query.setPId(folderId);
        query.setFileFlag("0"); // 只查询文件
        return zskService.selectDsZskList(query);
    }

    // 删除文件
    @DeleteMapping("/file/delete/{fileId}")
    public String deleteFile(@PathVariable Long fileId) {
        try {
            // 检查文件是否存在
            DsZsk file = zskService.selectDsZskByZskId(fileId);
            if (file == null) {
                return "File not found";
            }

            // 检查是否为文件（不是文件夹）
            if ("1".equals(file.getFileFlag())) {
                return "Cannot delete a folder using this endpoint";
            }

            // 删除文件
            zskService.deleteDsZskByZskId(fileId);
            return "File deleted successfully";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 删除文件夹（包括其中的所有文件）
    @DeleteMapping("/delete/{folderId}")
    public String deleteFolder(@PathVariable Long folderId) {
        try {
            // 检查文件夹是否存在
            DsZsk folder = zskService.selectDsZskByZskId(folderId);
            if (folder == null) {
                return "Folder not found";
            }

            // 检查是否为文件夹
            if (!"1".equals(folder.getFileFlag())) {
                return "Cannot delete a file using this endpoint";
            }

            // 删除文件夹中的所有文件
            DsZsk query = new DsZsk();
            query.setPId(folderId);
            List<DsZsk> files = zskService.selectDsZskList(query);
            for (DsZsk file : files) {
                zskService.deleteDsZskByZskId(file.getZskId());
            }

            // 删除文件夹
            zskService.deleteDsZskByZskId(folderId);
            return "Folder and its contents deleted successfully";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 修改文件夹名称
    @PutMapping("/rename/{folderId}")
    public String renameFolder(@PathVariable Long folderId, @RequestBody Map<String, String> requestBody) {
        try {
            // 检查文件夹是否存在
            DsZsk folder = zskService.selectDsZskByZskId(folderId);
            if (folder == null) {
                return "Folder not found";
            }

            // 获取新名称
            String newName = requestBody.get("name");
            if (newName == null || newName.trim().isEmpty()) {
                return "New name cannot be empty";
            }

            // 修改文件夹名称
            folder.setZskName(newName);
            zskService.updateDsZsk(folder);
            return "Folder renamed successfully";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 修改文件名称
    @PutMapping("/file/rename/{fileId}")
    public String renameFile(@PathVariable Long fileId, @RequestBody Map<String, String> requestBody) {
        try {
            // 检查文件是否存在
            DsZsk file = zskService.selectDsZskByZskId(fileId);
            if (file == null) {
                return "File not found";
            }

            // 获取新名称
            String newName = requestBody.get("name");
            if (newName == null || newName.trim().isEmpty()) {
                return "New name cannot be empty";
            }

            // 修改文件名称
            file.setZskName(newName);
            zskService.updateDsZsk(file);
            return "File renamed successfully";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
}
